package org.yogthos;

import java.util.LinkedList;
import java.util.List;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.view.MotionEvent;

/**
 * (c) 2010 Nicolas Gramlich (c) 2011 Zynga
 * 
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class DotChaserActivity extends BaseExample implements IOnSceneTouchListener {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	private static final float DEMO_VELOCITY = 100.0f;
	Penguin player;
	
	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;

	// private BitmapTextureAtlas bitmapTextureAtlas;
	private TiledTextureRegion yellowBallTexture;
	private TiledTextureRegion redBallTexture;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	public TiledTextureRegion loadTexture(String name, int columns, int rows) {
		BitmapTextureAtlas bitmapTextureAtlas = new BitmapTextureAtlas(64, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		TiledTextureRegion texture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas, this, name, 0, 0, columns, rows);
		this.mEngine.getTextureManager().loadTexture(bitmapTextureAtlas);
		return texture;
	}

	@Override
	public void onLoadResources() {

		this.yellowBallTexture = loadTexture("face_circle_tiled.png", 2, 1);
		this.redBallTexture = loadTexture("face_circle_tiled_red.png", 2, 1);
	}

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		
		
		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
		// Touch listener
		scene.setOnSceneTouchListener(this);
		

		final int centerX = (CAMERA_WIDTH - yellowBallTexture.getWidth()) / 2;
		final int centerY = (CAMERA_HEIGHT - yellowBallTexture.getHeight()) / 2;

		player = new Penguin(CAMERA_WIDTH, CAMERA_HEIGHT, centerX, centerY, 50, 50, redBallTexture);
		scene.attachChild(player);

		final List<Fish> fishes = new LinkedList<Fish>();
		for (int i = 0; i < 5; i++) {
			float fishPos = 50 + i * yellowBallTexture.getWidth() + numInRange(10, 20); 
			Fish fish = new Fish(CAMERA_WIDTH, centerX + numInRange(-100, 100), fishPos, DEMO_VELOCITY + numInRange(0, 100), yellowBallTexture);
			fishes.add(fish);
			scene.attachChild(fish);
		}

		scene.registerUpdateHandler(new IUpdateHandler() {

			@Override
			public void reset() {
			}

			@Override
			public void onUpdate(final float pSecondsElapsed) {				
				List<Fish> deadFish = new LinkedList<Fish>();
				for (Fish fish : fishes) {
					if (player.collidesWith(fish)) {
						player.setCurrentTileIndex(1);
						scene.detachChild(fish);
						deadFish.add(fish);
					}
				}
				
				for (Fish fish : deadFish) {
					fishes.remove(fish);
				}
				
			}
		});

		return scene;
	}
	
/*	 public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
	        int myEventAction = event.getAction(); 

	        float X = event.getX();
		float Y = event.getY();

	        switch (myEventAction) {
	           case MotionEvent.ACTION_DOWN:
	        	break;
	           case MotionEvent.ACTION_MOVE: {
	            	mySprite.setPosition(X, Y);
	            	break;}
	           case MotionEvent.ACTION_UP:
	                break;
	        }
		// pSceneTouchEvent.getX();
		 System.out.println("Touch");
	     return true;
	    }*/

	public static float numInRange(int min, int max) {
		return (float) (min + (int) (Math.random() * ((max - min) + 1)));
	}

	@Override
	public void onLoadComplete() {

	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		player.setVelosity(pSceneTouchEvent.getX(),pSceneTouchEvent.getY());
		return false;
	}
}
